package com.wc.算法提高课.A第一章_动态规划.单调队列优化DP.理想的正方形;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/14 0:02
 * @description https://www.acwing.com/problem/content/description/1093/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010, INF = 0x3f3f3f3f;
    static int[][] w = new int[N][N];
    // rowMax[i][j] 表示第i行 j - n + 1 ~ j中的行最大值, rowMin同理最小值
    static int[][] rowMax = new int[N][N], rowMin = new int[N][N];
    static int[] q = new int[N];
    static int n, m, k;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        k = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                w[i][j] = sc.nextInt();
            }
        }
        for (int i = 1; i <= n; i++) {
            getMax(w[i], rowMax[i], m);
            getMin(w[i], rowMin[i], m);
        }
        // a存一列当中的最大或者最小的
        int[] a = new int[N], b = new int[N], c = new int[N];
        int res = INF;
        for (int i = k; i <= m; i++) {
            // 拷贝列
            for (int j = 1; j <= n; j++) a[j] = rowMax[j][i];
            getMax(a, b, n);
            for (int j = 1; j <= n; j++) a[j] = rowMin[j][i];
            getMin(a, c, n);
            for (int j = k; j <= n; j++) res = Math.min(res, b[j] - c[j]);
        }
        out.println(res);
        out.flush();
    }

    static void getMax(int[] a, int[] b, int tot) {
        int hh = 1, tt = 0;
        for (int i = 1; i <= tot; i++) {
            if (hh <= tt && i - k >= q[hh]) hh++;
            while (hh <= tt && a[q[tt]] <= a[i]) tt--;
            q[++tt] = i;
            b[i] = a[q[hh]];
        }
    }

    static void getMin(int[] a, int[] b, int tot) {
        int hh = 1, tt = 0;
        for (int i = 1; i <= tot; i++) {
            if (hh <= tt && i - k >= q[hh]) hh++;
            while (hh <= tt && a[q[tt]] >= a[i]) tt--;
            q[++tt] = i;
            b[i] = a[q[hh]];
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}